/*
 * PhotoSetDAO.java
 *
 * Created on April 6, 2007, 10:50 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * This class has all the CRUD methods which interfaces with the database
 * @author uni
 */
public class PhotoSetDAO {
   private static PhotoSetDAO instance = null;
    private PreparedStatement insertPhotoSet = null;
    private PreparedStatement updatePhotoSet = null;
    private PreparedStatement readPhotoSet = null;
    
    /** Creates a new instance of PhotoSetDAO */
    public PhotoSetDAO() {
    }
    
   /**
     * This method implements the singleton pattern. It insures that only
     * on instance of InterestDAO exists
     * @return BackupDAO
     */
    public static synchronized PhotoSetDAO getInstance() {
        if (instance == null) {
            instance = new PhotoSetDAO();
        }
        return instance;
        
    }
    
    /**
     * This method creates a new RevenueSourceBO with a unique QUID, puts the photoset
     * in cache, and then returns it to the calling method.
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return BAckup BO
     */
    public PhotoSet create(String descriptionOfSet, int numInSet, String externalID,String membershipGuid)throws Exception {
        PhotoSet p = null;
        p = create(GUID.generate(), descriptionOfSet, numInSet, externalID, membershipGuid );
        Cache.getInstance().put(p.getId (),p);
        return p;
    }
    /**
     * create photoset
     */
    public PhotoSet create(String guid, String descriptionOfSet, int numInSet, String externalID,String membershipGuid)throws Exception {
        PhotoSet p = null;
        p = new PhotoSet(guid);
        p.setDescriptionOfSet (descriptionOfSet);
        p.setNumInSet (numInSet);
        p.setExternalID (externalID);
        p.setMemberID (membershipGuid);
        Cache.getInstance().put(p.getId (),p);
        return p;
    }
    
    /**
     * insert photoset
     */
    public void insertPhotoSet(PhotoSet p)throws Exception {
        Connection conn = ConnectionPool.getInstance().get();
        insertPhotoSet(p,conn);
    }
    /**
     * insert photoset
     */
    public void insertPhotoSet(PhotoSet p , Connection conn)throws Exception{
        try {
            this.insertPhotoSet = conn.prepareStatement("insert into photo_set values" +
                    "(?,?,?,?,?)");
            insertPhotoSet.setString(1,p.getId ());
            insertPhotoSet.setString(2,p.getDescriptionOfSet());
            insertPhotoSet.setInt (3,p.getNumInSet());
            insertPhotoSet.setString(4,p.getExternalID());
            insertPhotoSet.setString(5,p.getMemberID ());
            insertPhotoSet.execute();
            ConnectionPool.getInstance().release(conn);
        } catch (SQLException ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        }
    }
    
    
    /**
     * update photoset
     */
    public void updatePhotoSet(PhotoSet p)throws Exception{
        Connection conn = ConnectionPool.getInstance().get();
        updatePhotoSet(p,conn);
    }
    /**
     * update photoset
     */
    public void updatePhotoSet(PhotoSet p, Connection conn)throws Exception {
        try {
            this.updatePhotoSet = conn.prepareStatement("update photo_set set" +
                    " description_of_set = ?, num_in_set = ?, external_id = ?, memberID = ? where photo_set_guid = ?");
            updatePhotoSet.setString(1,p.getDescriptionOfSet());
            updatePhotoSet.setInt (2,p.getNumInSet ());
            updatePhotoSet.setString(3,p.getExternalID());
            updatePhotoSet.setString(4,p.getMemberID ());
            updatePhotoSet.setString(5,p.getId ());
            updatePhotoSet.executeUpdate();
            ConnectionPool.getInstance().release(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        }
    }
    
     /**
     * This is the public read statement.  It loads an existing record
     * from the database.
     * @param id id of photoset
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return BackupBO
     */
    public PhotoSet readPhotoSet(String guid)throws Exception{
        Connection conn = ConnectionPool.getInstance().get();
        PhotoSet p = readPhotoSet(guid,conn);
        return p;
    }
    
    /**
     *  This is a package method that is called by the public read (above) or
     *  by another DAO.  Either way we already have a connection to the database
     *  to use.  The user (controller) code never calls this one directly, since
     *  it can't know about Connection objects or SQLExceptions.
     * @param id id of Photoset
     * @param conn connection
     * @throws java.sql.SQLException 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return PhotoSet
     */
    public PhotoSet readPhotoSet(String guid, Connection conn)throws Exception {
        PhotoSet p = null;
        if(Cache.getInstance().containsKey(guid)){
            p = (PhotoSet)Cache.getInstance().get(guid);
            Cache.getInstance().touch(guid);
        }else{
            try {
                this.readPhotoSet = conn.prepareStatement("select * from photo_set where photo_set_guid = ?");
                readPhotoSet.setString(1,guid);
                ResultSet rs = readPhotoSet.executeQuery();
                while(rs.next()){
                    p = this.create(rs.getString("photo_set_guid"),
                            rs.getString ("description_of_set"),
                            rs.getInt("num_in_set"),
                            rs.getString("external_id"),rs.getString("memberID"));
                }
                ConnectionPool.getInstance().release(conn);
                
            } catch (Exception ex) {
                ex.printStackTrace();
                conn.rollback();
                ConnectionPool.getInstance().release(conn);
                throw ex;
            }
        }
        return p;
    }
    
    /**
     * read photoset from external id
     */
    public List <PhotoSet> readPhotoSetFromExternalID(String externalID)throws Exception{
        Connection conn = ConnectionPool.getInstance().get();
        List <PhotoSet> p = readPhotoSetFromExternalID(externalID,conn);
        return p;
    }
    
    /**
     * read photoset from external id
     */
    public List <PhotoSet> readPhotoSetFromExternalID(String externalID, Connection conn)throws Exception {
        List <PhotoSet> p = new LinkedList();
        PhotoSet temp = null;
        try {
            this.readPhotoSet = conn.prepareStatement("select * from photo_set where external_id = ?");
            readPhotoSet.setString(1,externalID);
            ResultSet rs = readPhotoSet.executeQuery();
            while(rs.next()){
                temp = this.create(rs.getString("photo_set_guid"),
                        rs.getInt ("num_in_set"),
                        rs.getString("external_id"),rs.getString("memberID"));
                p.add(temp);
            }
            ConnectionPool.getInstance().release(conn);
            
        } catch (Exception ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        }
        
        return p;
    }
    
    /**
     * read photoset from membership
     */
    public List <PhotoSet> readPhotoSetFromMembership(String membershipGuid)throws Exception{
        Connection conn = ConnectionPool.getInstance().get();
        List <PhotoSet> p = readPhotoSetFromMembership(membershipGuid,conn);
        return p;
    }
    
    /**
     * read photoset from membership
     */
    public List <PhotoSet> readPhotoSetFromMembership(String membershipGuid, Connection conn)throws Exception {
        List <PhotoSet> p = new LinkedList();
        PhotoSet temp = null;
        try {
            this.readPhotoSet = conn.prepareStatement("select * from photo_set where memberID = ?");
            readPhotoSet.setString(1,membershipGuid);
            ResultSet rs = readPhotoSet.executeQuery();
            while(rs.next()){
                temp = this.create(rs.getString("photo_set_guid"),
                        rs.getInt("num_in_set"),
                        rs.getString("external_id"),rs.getString("memberID"));
                p.add(temp);
            }
            ConnectionPool.getInstance().release(conn);
            
        } catch (Exception ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        }
        
        return p;
    }
 
}
